package com.firebird.app.kotlin.stock

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import butterknife.BindView
import butterknife.OnClick
import com.firebird.app.kotlin.stock.base.BaseActivity
//import com.sun.javaws.ui.SplashScreen.hide
import com.firebird.app.kotlin.stock.ui.index_fragment.account.AccountFragment
import com.firebird.app.kotlin.stock.ui.index_fragment.deal.DealFragment
import com.firebird.app.kotlin.stock.ui.index_fragment.index.IndexFragment
import com.firebird.app.kotlin.stock.ui.index_fragment.money.MoneyFragment
import java.util.*

class MainActivity : BaseActivity() {
    @BindView(R.id.fragment_main)
    var fragmentMain: LinearLayout? = null
    @BindView(R.id.index_icon)
    var indexIcon: ImageView? = null
    @BindView(R.id.index_name)
    var indexName: TextView? = null
    @BindView(R.id.bt_index_index)
    var btIndexIndex: RelativeLayout? = null
    @BindView(R.id.money_icon)
    var moneyIcon: ImageView? = null
    @BindView(R.id.money_name)
    var moneyName: TextView? = null
    @BindView(R.id.bt_index_money)
    var btIndexMoney: RelativeLayout? = null
    @BindView(R.id.deal_icon)
    var dealIcon: ImageView? = null
    @BindView(R.id.deal_name)
    var dealName: TextView? = null
    @BindView(R.id.bt_index_deal)
    var btIndexDeal: RelativeLayout? = null
    @BindView(R.id.account_icon)
    var accountIcon: ImageView? = null
    @BindView(R.id.account_name)
    var accountName: TextView? = null
    @BindView(R.id.bt_index_account)
    var btIndexAccount: RelativeLayout? = null
    private var fragment: Fragment? = null
    private var fragmentList: MutableList<Fragment>? = null
    private var currentIndex = 0
    private var fragmentManager: FragmentManager? = null
    private val exitTime: Long = 0
    override val layoutId: Int
        get() = R.layout.activity_main

    override fun initView() {
        fragment = Fragment()
        fragmentList = ArrayList<Fragment>()
        fragmentManager = supportFragmentManager
        if (savedInstanceState != null) {
            currentIndex = savedInstanceState!!.getInt(MainActivity.Companion.CURRENT_FRAGMENT, 0)
            fragmentList.removeAll(fragmentList)
            fragmentList.add(fragmentManager.findFragmentByTag(0.toString() + ""))
            fragmentList.add(fragmentManager.findFragmentByTag(1.toString() + ""))
            fragmentList.add(fragmentManager.findFragmentByTag(2.toString() + ""))
            restoreFragment()
        } else {
            fragmentList.add(IndexFragment())
            fragmentList.add(MoneyFragment())
            fragmentList.add(DealFragment())
            fragmentList.add(AccountFragment())
            showFragment()
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        outState.putInt(MainActivity.Companion.CURRENT_FRAGMENT, currentIndex)
        super.onSaveInstanceState(outState)
    }

    private fun showFragment() {
        val transaction: FragmentTransaction = fragmentManager.beginTransaction()
        if (!fragmentList!![currentIndex].isAdded()) {
            transaction
                    .hide(fragment)
                    .add(R.id.fragment_main, fragmentList!![currentIndex], "" + currentIndex)
        } else {
            transaction
                    .hide(fragment)
                    .show(fragmentList!![currentIndex])
        }
        fragment = fragmentList!![currentIndex]
        transaction.commit()
    }

    private fun restoreFragment() {
        val mBeginTreansaction: FragmentTransaction = fragmentManager.beginTransaction()
        for (i in fragmentList!!.indices) {
            if (i == currentIndex) {
                mBeginTreansaction.show(fragmentList!![i])
            } else {
                mBeginTreansaction.hide(fragmentList!![i])
            }
        }
        mBeginTreansaction.commit()
        fragment = fragmentList!![currentIndex]
    }

    @OnClick(R.id.bt_index_index, R.id.bt_index_money, R.id.bt_index_deal, R.id.bt_index_account)
    fun onViewClicked(view: View) {
        when (view.id) {
            R.id.bt_index_index -> {
                currentIndex = 0
                showFragment()
                indexName!!.setTextColor(resources.getColor(R.color.indexSelect))
                moneyName!!.setTextColor(resources.getColor(R.color.indexUnSelect))
                dealName!!.setTextColor(resources.getColor(R.color.indexUnSelect))
                accountName!!.setTextColor(resources.getColor(R.color.indexUnSelect))
                indexIcon!!.setImageResource(R.mipmap.index1)
                moneyIcon!!.setImageResource(R.mipmap.money0)
                dealIcon!!.setImageResource(R.mipmap.deal0)
                accountIcon!!.setImageResource(R.mipmap.account0)
            }
            R.id.bt_index_money -> {
                currentIndex = 1
                showFragment()
                indexName!!.setTextColor(resources.getColor(R.color.indexUnSelect))
                moneyName!!.setTextColor(resources.getColor(R.color.indexSelect))
                dealName!!.setTextColor(resources.getColor(R.color.indexUnSelect))
                accountName!!.setTextColor(resources.getColor(R.color.indexUnSelect))
                indexIcon!!.setImageResource(R.mipmap.index0)
                moneyIcon!!.setImageResource(R.mipmap.money1)
                dealIcon!!.setImageResource(R.mipmap.deal0)
                accountIcon!!.setImageResource(R.mipmap.account0)
            }
            R.id.bt_index_deal -> {
                currentIndex = 2
                showFragment()
                indexName!!.setTextColor(resources.getColor(R.color.indexUnSelect))
                moneyName!!.setTextColor(resources.getColor(R.color.indexUnSelect))
                dealName!!.setTextColor(resources.getColor(R.color.indexSelect))
                accountName!!.setTextColor(resources.getColor(R.color.indexUnSelect))
                indexIcon!!.setImageResource(R.mipmap.index0)
                moneyIcon!!.setImageResource(R.mipmap.money0)
                dealIcon!!.setImageResource(R.mipmap.deal1)
                accountIcon!!.setImageResource(R.mipmap.account0)
            }
            R.id.bt_index_account -> {
                currentIndex = 3
                showFragment()
                indexName!!.setTextColor(resources.getColor(R.color.indexUnSelect))
                moneyName!!.setTextColor(resources.getColor(R.color.indexUnSelect))
                dealName!!.setTextColor(resources.getColor(R.color.indexUnSelect))
                accountName!!.setTextColor(resources.getColor(R.color.indexSelect))
                indexIcon!!.setImageResource(R.mipmap.index0)
                moneyIcon!!.setImageResource(R.mipmap.money0)
                dealIcon!!.setImageResource(R.mipmap.deal0)
                accountIcon!!.setImageResource(R.mipmap.account1)
            }
        }
    }

    companion object {
        private const val CURRENT_FRAGMENT = "STATE_FRAGMENT_SHOW"
    }
}